package com.rainy.cloud.core.exception.handler;

import com.rainy.cloud.core.exception.definition.*;
import com.rainy.cloud.entity.ResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GlobalExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public ResultVo<Object> exceptionHandler(Exception exception) {
        ResultVo resultVo = new ResultVo();
        if (exception instanceof ValidateException) {
            ValidateException exceptionTmp = (ValidateException) exception;
            resultVo.setCode(exceptionTmp.getCode());
            resultVo.setErrMsg(exceptionTmp.getMessage());
            resultVo.setData("");
            logger.error("验证异常:{}", exception);
        } else if (exception instanceof BizException) {
            BizException exceptionTmp = (BizException) exception;
            resultVo.setCode(exceptionTmp.getCode());
            resultVo.setErrMsg(exceptionTmp.getMessage());
            resultVo.setData("");
            logger.error("业务异常:{}", exception);
        } else if (exception instanceof ServerException) {
            ServerException exceptionTmp = (ServerException) exception;
            resultVo.setCode(exceptionTmp.getCode());
            resultVo.setErrMsg(exceptionTmp.getMessage());
            resultVo.setData("");
            logger.error("服务异常:{}", exception);
        } else if (exception instanceof ApplicationException) {
            ApplicationException exceptionTmp = (ApplicationException) exception;
            resultVo.setCode(exceptionTmp.getCode());
            resultVo.setErrMsg(exceptionTmp.getMessage());
            resultVo.setData("");
            logger.error("应用异常:{}", exception);
        } else if (exception instanceof UnknownException) {
            UnknownException exceptionTmp = (UnknownException) exception;
            resultVo.setCode(exceptionTmp.getCode());
            resultVo.setErrMsg(exceptionTmp.getMessage());
            resultVo.setData("");
            logger.error("未知异常:{}", exception);
        }
        return resultVo;
    }

}
